<?
class Url{
	static function build_all($except=array(), $addition=false){
		if(is_array($except))
			$except+=array('form_block_id','debug','tbug','delcache','delscache','utm_source','utm_campaign','utm_content','utm_medium');
		else
			$except=array('form_block_id','debug','tbug','delcache','delscache','utm_source','utm_campaign','utm_content','utm_medium');

		$url  = '';
		$page = '';
		$arr = array();
		foreach($_POST as $key=>$value){
			if(!in_array($key, $except) && $key!='form_block_id'){
				if(!is_array($value)){
					if($key=='page'){
						$page=urlencode($value);
					}
					else{
						$url.=($url?'&':'').urlencode($key).'='.urlencode($value);
						$arr[$key] = $value;
					}
				}
			}
		}

		foreach($_GET as $key=>$value){

			if(!in_array($key, $except) && $key!='form_block_id' && !isset($_POST[$key])){
				if($key=='page'){
					$page=urlencode($value);
				}
				else{
					$url.=($url?'&':'').urlencode($key).'='.urlencode($value);
					$arr[$key] = $value;
				}
			}
		}

		if($addition){
			$url.=($url?'&':'').$addition;
		}

		if(!$page || $page=='home'){
			$url=($url?'?':'').$url;
		}
		else{
			$url='?page='.$page.($url?'&'.$url:'');
		}
		return Url::build($page,$arr);
	}

	static function build_current($params=array(),$anchor=''){
		$pageName = Url::get('page','');
		return Url::build($pageName,$params,$anchor);
	}

	static function build($page,$params=array(),$anchor=''){
		$request_string = '';
		$ext = ($page) ? '.html' : '';
		$other_params ='';
		if(!REWRITE_ON){
				if($page=='home')
						$request_string = '';
				else{
					$request_string = '?page='.$page;
				}

				if ($params){
					foreach ($params as $param=>$value){
						if(is_numeric($param)){
							if(isset($_REQUEST[$value]) && $value!='page'){
								$request_string .= ($request_string?'&':'?').$value.'='.urlencode($_REQUEST[$value]);
							}
						}
						elseif($param!='page'){
							$request_string .= ($request_string?'&':'?').$param.'='.urlencode($value);
						}
					}
				}
		}
		else{
			//get list page
			$array_page = array();
			if(!empty(CGlobal::$arrPage)){
				foreach(CGlobal::$arrPage as $row){
					$array_page[$row['name']] = $row['rewrite'] ? $row['rewrite'] : $row['name'];
				}
			}
			if ($params){
					$params_order = array();
					$prefix_params = array();
					//$default_prefix_params = array('page_no' =>'trang');
					// thu tu cac param truyen vao theo tung trang
					switch ($page){
						case 'profile':
							$params_order[$page] = array('user');
						break;
						case 'trang_tinh':
							$params_order[$page] = array('tmp');
						break;
						case 'product':
						case 'comment':
							$params_order[$page] = array('id','title');
							$prefix_params[$page] = array('id' => 'p');
						break;
						case 'admin':
							$params_order[$page] = array('cmd','action');
						break;
					}
					$new_params = (isset($params_order[$page])) ? array_fill_keys($params_order[$page], '') : array();
					$new_params['other_params'] = '';
					foreach ($params as $param=>$value){
							$new_value = '';
							if(is_numeric($param)){ // truong hop chi truyen vao param khong co gia tri di kem thi lay gia tri dang co tren url

								if(isset($_REQUEST[$value]) && $value!='page'){
										$param = $value;
										$value = $_REQUEST[$value];
								}
								else{
									$param = '';
									$value = '';
								}
							}
							if($param!='page' && $param != '' && $value != ''){
								$value = urlencode($value);

								if(isset($prefix_params[$page][$param])){
									if($prefix_params[$page][$param] == '|'){
										$new_value = $value;
									}else{
										$new_value = $prefix_params[$page][$param].'-'.$value;
									}
								}
								elseif(isset($default_prefix_params[$param])){
									$new_value = $default_prefix_params[$param].'-'.$value;
								}
								else{
									if(!isset($new_params[$param])){ // neu param khong ton tai trong mang sap xep
										$new_params['other_params'] .= ($new_params['other_params'])?'&':'?';
										$new_params['other_params'] .= $param.'='.$value;
									}
									else{
										$new_value = $value;
									}
								}

							}
							if($new_value){
								$new_params[$param] = $new_value;
							}

					}

					//check trang can add them ten trang vao

					foreach ($new_params as $key=>$value){
							if($value && $key!='other_params' && isset($params_order[$page])){
									$request_string .= ($request_string) ? '/' : '';
									$request_string .= $value;
							}

					}
					if($page=='profile' && count($params) == 1 && isset($params['user'])){
								$request_string = $request_string.$new_params['other_params'];
					}
					else{
						if($request_string){
								$page = (isset($array_page[$page])) ? $array_page[$page] : $page;
								$request_string = $page.'/'.$request_string.$ext.$new_params['other_params'];
						}
						else{ // neu khong dc rewrite
								$page = (isset($array_page[$page])) ? $array_page[$page] : $page;
								$request_string = $page.$ext.$new_params['other_params'];
						}
					}
			}
			else{
					if($page=='home')
							$request_string = '';
					else{
							$request_string = (isset($array_page[$page])) ? $array_page[$page].$ext : $page.$ext;
					}


			}
		}
		return WEB_ROOT.$request_string.$anchor;
	}


	static function redirect_current($params=array(),$anchor = ''){
		Url::redirect(Layout::$page['name'],$params,$anchor);
	}

	static function redirect_href($params=false){
		if(Url::check('href')){
			Url::redirect_url(Url::attach($_REQUEST['href'],$params));
			return true;
		}
	}

	static function check($params){
		if(!is_array($params)){
			$params=array(0=>$params);
		}

		foreach($params as $param=>$value){
			if(is_numeric($param)){
				if(!isset($_REQUEST[$value])){
					return false;
				}
			}
			else{
				if(!isset($_REQUEST[$param])){
					return false;
				}
				else{
					if($_REQUEST[$param]!=$value){
						return false;
					}
				}
			}
		}
		return true;
	}

	//Chuyen sang trang chi ra voi $url
	static function redirect($page=false,$params=false, $anchor=''){
		if(!$page && !$params){
			Url::redirect_url();
		}
		else{
			Url::redirect_url(Url::build($page, $params,$anchor));
		}
	}

	static function redirect_url($url=false){
		if($url != ''){
			if(strpos($url,WEB_ROOT) === 0){
				$url = substr($url,strlen(WEB_ROOT));
			}
		}
		else{
			$url = '';
		}

		header('Location:'.WEB_ROOT.$url);
		System::halt();
	}

	static function access_denied(){
		Url::redirect('home');
		//header("Location: ".WEB_ROOT."err/error.html");
		//die();
	}

	static function get($name,$default=''){
		if (isset($_REQUEST[$name])){
			return $_REQUEST[$name];
		}
		elseif (isset($_POST[$name])){
			return $_POST[$name];
		}
		elseif(isset($_GET[$name])){
			return $_GET[$name];
		}
		else{
			return $default;
		}
	}

	static function sget($name,$default=''){
		return strtr(Url::get($name, $default),array('"'=>'\\"'));
	}

	static function cdouble( $doubleval ){
	//echo $doubleval;
		if(!is_numeric($doubleval))
		{

			$doubleval=strtr($doubleval,array('.'=>''));
			$doubleval=strtr($doubleval,array(','=>'.'));

		}
		return doubleval($doubleval);
	}
}
?>